<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/31
 * Time: 9:30
 */

namespace app\admin\controller;


class Promotion extends Base
{
    /**
     * 优惠券管理
     */
    public function coupon(){
        $list = model('Coupon') -> paginate(10);

        $this -> assign('list', $list);
        return $this -> fetch();
    }

    public function coupon_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Coupon') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('promotion/coupon'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $res = model('Coupon') -> resFind($id);
            }

            $this -> assign('id', $id);
            $this -> assign('res', $res);
            return $this -> fetch();
        }
    }

    public function coupon_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Coupon') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('promotion/coupon'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 优惠券状态
     */
    public function coupon_status(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Coupon') -> editStatus($data);

            if($result['code']){
                return $this -> success($result['msg'], url('promotion/coupon'), ['status' => $data['status']]);
            }else{
                return $this -> error($result['msg']);
            }
        }else{
            return $this -> error('非法请求');
        }
    }

    /**
     * 查看拥有的优惠券的用户
     * 可操作 删除
     */
    public function coupon_cat(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = $this -> validate($data, 'Coupon.del');
            if(true !== $result){
                return $this -> error($result, '', ['token' => $this -> request -> token()]);
            }

            $res = model('CouponList') -> del($data['id']);
            if($res['code']){
                return $this -> success($res['msg'], url('promotion/coupon_cat'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            $list = model('CouponList') -> resList($id);

            $this -> assign('list', $list);
            return $this -> fetch();
        }
    }

    /**
     * 发放优惠券
     */
    public function coupon_send(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('CouponList') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('promotion/coupon'), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);

            $res = null;
            if($id){
                $users = model('CouponList') -> resFind($id);

                $this -> assign('id', $id);
                $this -> assign('users', $users);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求');
            }
        }
    }
}